package com.xwj.aop;

import com.xwj.ioc.BeanPostProcessor;
import com.xwj.ioc.factory.BeanFactory;
import com.xwj.ioc.factory.BeanFactoryAware;
import com.xwj.ioc.xml.XmlBeanFactory;
import org.aopalliance.intercept.MethodInterceptor;

/**
 * @author xwj
 * @date 2020/4/11
 */
public class AspectJAwareAdvisorAutoProxyCreator implements BeanPostProcessor, BeanFactoryAware {
    private XmlBeanFactory xmlBeanFactory;

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws Exception {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws Exception {
        /* 这里两个 if 判断很有必要，如果删除将会使程序进入死循环状态，
         * 最终导致 StackOverflowError 错误发生
         */
        if (bean instanceof AspectJExpressionPointcutAdvisor) {
            return bean;
        }
        if (bean instanceof MethodInterceptor) {
            return bean;
        }
        return null;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws Exception {
        this.xmlBeanFactory = (XmlBeanFactory) beanFactory;
    }
}
